package com.crm.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.PowerMapper;
import com.crm.model.mpojo.LoginNum;
import com.crm.model.pojo.Complaint;
import com.crm.services.CareServices;
import com.crm.services.ComplaintServices;
import com.crm.services.GatherguestService;
import com.crm.services.LoginNumServices;
import com.crm.services.OrderInfoServers;
import com.crm.services.PowerService;
import com.crm.services.RefundgoodService;
import com.crm.services.SysDeptServices;
import com.crm.services.TaskServices;
import com.crm.services.UserServices;
import com.crm.util.LoginUtil;
import com.crm.util.MapmenusUtil;

@Controller
public class LoginAndHomepageController {

	@Autowired
	UserServices userService;
	@Autowired
	SysDeptServices sysDeptServices;
	@Autowired
	PowerMapper powerMapper;
	@Autowired
	PowerService powerService;
	@Autowired
	LoginNumServices loginNumServices;
	@Autowired
	GatherguestService gatherguestService;
	@Autowired
	OrderInfoServers orderInfoServers;
	@Autowired
	CareServices careservices;
	@Autowired
	ComplaintServices complaintServices;
	@Autowired
	TaskServices taskeServices;
	@Autowired
	RefundgoodService refundgoodMapper;

	/**
	 * 测试登录界面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/login/log", method = { RequestMethod.GET })
	public void login() {
		System.out.println("登录！");
	}

	@RequestMapping("/login")
	public String login11() {
		return "login";
	}

	@RequestMapping("/loginUser")
	public String loginUser(@RequestParam("username") String username, @RequestParam("password") String password,
			HttpSession session) {
		int loginUser = userService.loginUser(username, password, session);
		if (loginUser == 1) {
			LoginNum loginNum = new LoginNum();
			loginNum.setLnUid(LoginUtil.getUser().getuId());
			loginNum.setLnDate(new Date());
			loginNumServices.add(loginNum);
			return "redirect:index";
		}
		return "redirect:login/log";
	}

	/**
	 * 测试登录界面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/login/log1", method = { RequestMethod.GET })
	public void login1() {
		System.out.println("登录！");

	}

	@RequestMapping(value = "/login/log", method = { RequestMethod.POST })
	public String login2() {
		System.out.println("登录跳转！");
		return "redirect:/#/index";
	}

	/**
	 * 用于主页带链接刷新
	 * @param url
	 * @return
	 */
	@PostMapping("/indexurl")
	@ResponseBody
	public String indexurl(@RequestParam("url") String url) {
		System.out.println(url);
		String[] split = url.split("#");
		System.out.println(split);
		if (split.length != 2) {

		} else {
			if (split.length > 2) {

			}
		}
		return "123";
	}

	/**
	 * 测试主页
	 * 
	 * @param model
	 * @return 
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView homepage() {
		ModelAndView mv = new ModelAndView("/index");
		List<MapmenusUtil> list = powerService.menusList(LoginUtil.getUser().getuId());
		
		mv.addObject("menus", JSONObject.toJSONString(list));//左菜单
		mv.addObject("loginNum", loginNumServices.selectNumToday());//今日访问量
		mv.addObject("getcustNum", gatherguestService.selectgetcustNum());//今日获客数
		mv.addObject("orderNum", orderInfoServers.selectOrderNum());//今日订单数
		mv.addObject("careNum", careservices.selectcareNum());//今日关怀量
		mv.addObject("complaintNum", complaintServices.selectcomplaintNum());//今日投诉量
		mv.addObject("allCount", taskeServices.AllTasksCount());//后台各表总数
		mv.addObject("allChance", taskeServices.allChance());//销售各阶段跟踪数
		mv.addObject("yearProfile", refundgoodMapper.yearProfile());//上一年度盈利信息
		System.out.println("主页！");
		return mv;
	}

	/**
	 * 测试主页其他
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/homepage/head")
	public void head() {
		System.out.println("主页！");
	}

	@RequestMapping("/homepage/left")
	public void left() {
		System.out.println("主页！");
	}

	@RequestMapping("/homepage/main")
	public void main() {
		System.out.println("主页！");
	}

	/**
	 * 测试主页
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/index1")
	public ModelAndView homepage1() {
		ModelAndView mv = new ModelAndView("/index1");
		List<MapmenusUtil> list = powerService.menusList(LoginUtil.getUser().getuId());
		
		mv.addObject("menus", JSONObject.toJSONString(list));
		System.out.println("主页！");
		return mv;

	}

	/**
	 * 修改主页
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/index2")
	public void homepage2() {
		System.out.println("主页！");
	}

	/**
	 * 测试主页其他
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/homepage/head1")
	public void head1() {
		System.out.println("主页！");
	}

	@RequestMapping("/homepage/left1")
	public void left1() {
		System.out.println("主页！");
	}

	@RequestMapping("/homepage/main1")
	public void main1() {
		System.out.println("主页！");
	}
}
